import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

public class TestConcurrentHashmap {
    private static HashMap<Integer, String> map = new HashMap<>();
    private static ConcurrentHashMap<Integer, String> concurMap = new ConcurrentHashMap<>();

    public static void testNormalMap() {
        map.put(666666, "V");
        Thread t = new Thread(new TestTask1());
        t.start();
        int i = 0;
        while (i < 666666) {
            if(map.get(666666) == null){
                System.out.println("HashMap got null value  while read and write happens concurrently");
                return;
            }
            i++;
        }
    }
    public static void testConcurMap() {
        concurMap.put(666666, "V");
        Thread t = new Thread(new TestTask1());
        t.start();
        int i = 0;
        while (i < 666666) {
            if(concurMap.get(666666) == null){
                System.out.println("ConcurrentHashMap got null value for already put key while read and write happens concurrently");
                return;
            }
            i++;
        }
        System.out.println("ConcurrentHashMap won't get null value for already put key when read&write concurrently ");
    }

    static class TestTask1 implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 666666; i++) {
                map.put(i,"some v");
            }
        }
    }


    public static void main(String[] args) {
        testNormalMap();
        testConcurMap();
    }
}
